package com.share.device.controller;

import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.device.domain.Cabinet;
import com.share.device.service.ICabinetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * Author:pengyujia
 * Date: 2025/6/2 17:12
 * Description:
 */
@Tag(name = "充电宝柜机接口管理")
@RestController
@RequestMapping("/cabinet")
public class CabinetController extends BaseController {

    @Autowired
    private ICabinetService cabinetService;

    //分页查询
    @Operation(summary = "柜机分页查询接口")
    @GetMapping("/list")
    public TableDataInfo list(Cabinet cabinet) {
        startPage();
        List<Cabinet> list = cabinetService.selectListCabinet(cabinet);
        TableDataInfo dataTable = getDataTable(list);
        return dataTable;
    }

    @Operation(summary = "根据ID查询详情数据")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(cabinetService.getById(id));
    }

    @Operation(summary = "添加柜机接口")
    @PostMapping
    public AjaxResult add(@RequestBody Cabinet cabinet) {
        boolean flag = cabinetService.save(cabinet);
        AjaxResult ajax = toAjax(flag);
        return ajax;
    }

    @Operation(summary = "修改")
    @PutMapping
    public AjaxResult update(@RequestBody Cabinet cabinet) {
        boolean flag = cabinetService.updateById(cabinet);
        AjaxResult ajax = toAjax(flag);
        return ajax;
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        boolean flag = cabinetService.removeByIds(Arrays.asList(ids));
        AjaxResult ajax = toAjax(flag);
        return ajax;
    }

    @Operation(summary = "搜索未使用柜机")
    @GetMapping("/searchNoUseList/{keyword}")
    public AjaxResult searchNoUseList(@PathVariable String keyword) {
        return success(cabinetService.searchNoUseList(keyword));
    }

    @Operation(summary = "获取充电宝柜机全部详细信息")
    @GetMapping(value = "/getAllInfo/{id}")
    public AjaxResult getAllInfo(@PathVariable("id") Long id){
        return success(cabinetService.getAllInfo(id));
    }
}
